<?php
/**
 * @title 1631. 最小体力消耗路径
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $heights
     * @return Integer
     */
    function minimumEffortPath($heights) {
        /**
         * @since 2023-12-11 行数，列数
         */
        $rows = count($heights);
        $cols = count($heights[0]);

        /**
         * @since 2023-12-11 初始化各个点，到达该点所需体力
         */
        $effortArray = array_fill(0, $rows, array_fill(0, $cols, PHP_INT_MAX));

        /**
         * @since 2023-12-11 阻止回到起点
         */
        $effortArray[0][0] = 0;

        /**
         * @since 2023-12-11 逐层遍历
         */
        $nodeArray = [[0,0]];
        while($nodeArray){
            /**
             * @since 2023-12-11 下次遍历的点集合
             */
            $nextNodeArray = [];

            /**
             * @since 2023-12-11 遍历当前点
             */
            foreach ($nodeArray as list($r, $c)){
                /**
                 * @since 2023-12-11 [$r, $c]点所在高度，和已耗费体力
                 */
                $pointHeight = $heights[$r][$c];
                $pointEffort = $effortArray[$r][$c];

                /**
                 * @since 2023-12-11 向上移动
                 */
                if(isset($heights[$r-1][$c])){
                    /**
                     * @since 2023-12-11 到达向上的点，所需体力
                     */
                    $moveEffort = max($pointEffort, abs($heights[$r-1][$c]-$pointHeight));

                    /**
                     * @since 2023-12-11 小于向上点的体力，移动
                     */
                    if($moveEffort < $effortArray[$r-1][$c]){
                        $effortArray[$r-1][$c] = $moveEffort;
                        $nextNodeArray[] = [$r-1, $c];
                    } else {}
                } else {}

                /**
                 * @since 2023-12-11 向下移动
                 */
                if(isset($heights[$r+1][$c])){
                    /**
                     * @since 2023-12-11 到达向下的点，所需体力
                     */
                    $moveEffort = max($pointEffort, abs($heights[$r+1][$c]-$pointHeight));

                    /**
                     * @since 2023-12-11 小于向下点的体力，移动
                     */
                    if($moveEffort < $effortArray[$r+1][$c]){
                        $effortArray[$r+1][$c] = $moveEffort;
                        $nextNodeArray[] = [$r+1, $c];
                    } else {}
                } else {}

                /**
                 * @since 2023-12-11 向左移动
                 */
                if(isset($heights[$r][$c-1])){
                    /**
                     * @since 2023-12-11 到达向左的点，所需体力
                     */
                    $moveEffort = max($pointEffort, abs($heights[$r][$c-1]-$pointHeight));

                    /**
                     * @since 2023-12-11 小于向左点的体力，移动
                     */
                    if($moveEffort < $effortArray[$r][$c-1]){
                        $effortArray[$r][$c-1] = $moveEffort;
                        $nextNodeArray[] = [$r, $c-1];
                    } else {}
                } else {}

                /**
                 * @since 2023-12-11 向右移动
                 */
                if(isset($heights[$r][$c+1])){
                    /**
                     * @since 2023-12-11 到达向右的点，所需体力
                     */
                    $moveEffort = max($pointEffort, abs($heights[$r][$c+1]-$pointHeight));

                    /**
                     * @since 2023-12-11 小于向右点的体力，移动
                     */
                    if($moveEffort < $effortArray[$r][$c+1]){
                        $effortArray[$r][$c+1] = $moveEffort;
                        $nextNodeArray[] = [$r, $c+1];
                    } else {}
                } else {}
            }

            /**
             * @since 2023-12-11 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return
         */
        return $effortArray[$rows-1][$cols-1];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.11.php
 */
$datas = [
    [[[1,2,2],[3,8,2],[5,3,5]]], // 2
    [[[1,2,3],[3,8,4],[5,3,5]]], // 1
    [[[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]], // 0
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minimumEffortPath(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
